//
//  WmcMoreRightAlert.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/7/7.
//

import UIKit
import YPBedrock

// MARK: - [WmcMoreRightAlert]

extension WmcHomeMoreRightsAlert {
    enum Style {
        /// 会员相机未登录
        case watermarkCameraNotLogin
        /// 鱼泡首次拍照
        case yuPaoFirstTakePhoto

        struct Info {
            var title: String
            var subtTitle: String
        }

        var info: Info {
            switch self {
            case .yuPaoFirstTakePhoto: return .init(title: "已解锁以下权益", subtTitle: "去看看")
            case .watermarkCameraNotLogin: return .init(title: "登录解锁更多权益", subtTitle: "去登录")
            }
        }
    }

    struct Model {
        /// 图片
        var img: String
        /// 标题
        var title: String
        /// 副标题
        var subTitle: String
    }

    /// 弹窗触发事件类型
    enum ActionType {
        // 拍照
        case takePhoto
        // 录视频
        case recodingVideo
        // 上传
        case uploadPhoto
    }
}

// MARK: - [WmcHomeMoreRightsAlert]

class WmcHomeMoreRightsAlert: WmcBaseView {
    // Internal

    var actionType: ActionType?

    lazy var deleteBtn: UIButton = {
        let view = UIButton()
        view.setImage("rights_delete".wmc_image, for: .normal)
        return view
    }()

    lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.wmc_white_FFFFFF
        view.layer.cornerRadius = 8
        view.clipsToBounds = true
        return view
    }()

    lazy var topBgImgView: UIImageView = {
        let view = UIImageView()
        view.image = "rights_bg_img".wmc_image
        return view
    }()

    lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.text = "已解锁以下权益"
        view.textColor = UIColor.wmc_blue_1D4182
        view.font = 22.yp.uiFont(weight: .semibold)
        return view
    }()

    lazy var collectionView: UICollectionView = {
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.register(WmcHomeMoreRightsAlertCell.self, forCellWithReuseIdentifier: "cell")
        view.delegate = self
        view.dataSource = self
        view.isScrollEnabled = false
        view.backgroundColor = UIColor.white
        return view
    }()

    lazy var layout: UICollectionViewFlowLayout = {
        let lay = UICollectionViewFlowLayout()
        lay.itemSize = getItemSize()
        lay.minimumLineSpacing = 0
        return lay
    }()

    lazy var loginBtn: UIButton = {
        let view = UIButton()
        view.setTitleColor(UIColor.wmc_white_FFFFFF, for: .normal)
        view.backgroundColor = UIColor.wmc_mainTheme
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()

    /// 弹窗
    /// - Parameter style: 类型
    /// - Returns: description
    class func alert(style: WmcHomeMoreRightsAlert.Style) -> WmcHomeMoreRightsAlert {
        let alert = WmcHomeMoreRightsAlert()
        alert.titleLabel.text = style.info.title
        alert.loginBtn.setTitle(style.info.subtTitle, for: .normal)
        return alert
    }

    /// 弹窗
    /// - Parameters:
    ///   - style: 类型
    ///   - btnAction: 按钮点击回调事件
    /// - Returns: description
    class func alert(style: WmcHomeMoreRightsAlert.Style, btnAction: (() -> Void)?) -> WmcHomeMoreRightsAlert {
        let alert = Self.alert(style: style)
        alert.btnAction = btnAction
        return alert
    }

    override func makeUI() {
        backgroundColor = UIColor.clear
        addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.height.equalTo(32)
        }
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.top.equalTo(52)
            make.left.right.bottom.equalToSuperview()
        }

        contentView.addSubview(topBgImgView)
        topBgImgView.addSubview(titleLabel)
        contentView.addSubview(collectionView)
        contentView.addSubview(loginBtn)

        topBgImgView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(80)
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(topBgImgView.snp.bottom).offset(27)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(94)
        }
        loginBtn.snp.makeConstraints { make in
            make.top.equalTo(collectionView.snp.bottom).offset(36)
            make.centerX.equalToSuperview()
            make.width.equalTo(165)
            make.height.equalTo(40)
            make.bottom.equalTo(-24)
        }
    }

    override func bindViewAction() {
        deleteBtn.yp.addEvent() { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        loginBtn.yp.addEvent() { [weak self] _ in
            guard let weakSelf = self, let actionBlock = weakSelf.btnAction else { return }
            weakSelf.dismiss(animated: true, completion: {
                actionBlock()
            })
        }
    }

    // Private

    private var btnAction: (() -> Void)?

    /// 获取数据源
    private var list: [Model] = [
        Model(img: "rights_clouldSave", title: "云端存储", subTitle: "不丢失"),
        Model(img: "rights_upload", title: "高清照片", subTitle: "上传快"),
        Model(img: "rights_free", title: "永久免费", subTitle: "大空间"),
    ]

    /// cell 大小
    /// - Returns: description
    private func getItemSize() -> CGSize {
        let width = ((Wmc_screenWidth * 0.85333) - 47 * 2) / 3.0
        let height: CGFloat = 94
        return CGSize(width: width, height: height)
    }
}

// MARK: - UICollectionViewDelegate, UICollectionViewDataSource

extension WmcHomeMoreRightsAlert: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return list.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! WmcHomeMoreRightsAlertCell
        if let model = list.yp.object(at: indexPath.row) {
            cell.configure(model: model)
        }
        return cell
    }
}

// MARK: - WmcAlertViewProtocol

extension WmcHomeMoreRightsAlert: WmcAlertViewProtocol {
    /// 显示
    func show() {
        var config: WmcPresentVC.Config =
            .basicConfig(view: self, style: .alert(), layoutBlock: { make in
                make.center.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(0.85333)
                make.height.greaterThanOrEqualTo(348)
            })
        config.isTouchMaskDismiss = false
        show(config: config)
    }
}
